package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.UserCouponQuery;
import cn.org.eshow.model.User;
import cn.org.eshow.model.UserCoupon;
import cn.org.eshow.service.UserCouponManager;
import cn.org.eshow.webapp.action.response.UserCouponResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券API接口
 */
@AllowedMethods({"mine"})
public class UserCouponAction extends ApiBaseAction<UserCoupon> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private UserCouponManager userCouponManager;

    private UserCoupon userCoupon = new UserCoupon();
    private List<UserCoupon> userCoupons = new ArrayList<UserCoupon>();
    private UserCouponQuery query = new UserCouponQuery(Boolean.TRUE);

    /**
     * 我的优惠券列表
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        userCoupons = userCouponManager.list(query);
        if (userCoupons.isEmpty()) {
            RenderUtil.noData();
            return;
        }
        List<UserCouponResponse> responses = new ArrayList<UserCouponResponse>();
        for (UserCoupon obj : userCoupons) {
            responses.add(new UserCouponResponse(obj));
        }
        RenderUtil.list("获取成功", "userCoupons", responses);
    }

    public UserCoupon getUserCoupon() {
        return userCoupon;
    }

    public void setUserCoupon(UserCoupon userCoupon) {
        this.userCoupon = userCoupon;
    }

    public List<UserCoupon> getUserCoupons() {
        return userCoupons;
    }

    public void setUserCoupons(List<UserCoupon> userCoupons) {
        this.userCoupons = userCoupons;
    }

    public UserCouponQuery getQuery() {
        return query;
    }

    public void setQuery(UserCouponQuery query) {
        this.query = query;
    }

}